package model.bullet;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;

import model.cell.tower.AbstractTower;
import model.enemy.AbstractEnemy;


public class LaserBullet extends AbstractBullet {
	
	protected int persistance = 40;
	
	protected int displayDuration = 0;
	
	
	
	public LaserBullet(Point pos, AbstractEnemy enemy, AbstractTower tower) {
		super(pos, enemy, tower);
		_damage = 30;
		color = Color.GREEN;
		_refreshSpeed = 10;
	}
	
	
	private AlphaComposite makeComposite(float alpha) {
		
		int type = AlphaComposite.SRC_OVER;
		return(AlphaComposite.getInstance(type, alpha));
	}
	
	
	public void draw(Graphics g) {
		
		Graphics2D g2d = (Graphics2D) g;
		
    	Composite originalComposite = g2d.getComposite();
    	float alpha = (float) (0.2 + ((float)displayDuration * 8) / ((float)persistance * 10));
    	
    	g2d.setComposite(makeComposite(alpha));
    	g2d.setPaint(color);
		g2d.setStroke(new BasicStroke( 2.0f ));
		
		Rectangle towerRect = _tower.getRectangle();
		Point enemyPos = _enemy.getPosition();
		g2d.drawLine(towerRect.x + towerRect.width / 2, towerRect.y + towerRect.height / 2, enemyPos.x, enemyPos.y);

		g2d.setComposite(originalComposite);
	}
	
	
	public synchronized void move() {
		
		if (_enemy == null || _enemy.getHealth() == 0) { 
			_destroy();
			return;
		}
		
		if ( displayDuration < persistance ) {
			displayDuration++;
			return;
		}
		
		_hitEnemy();
		displayDuration = 0;
		_destroy();
	}
	
	
	public void setDamage(int damage) {
		
		_damage = damage;
	}
	
}
